<?php
/**
 * This is the template for generating CRUD search class of the specified model.
 */

use yii\helpers\StringHelper;


/* @var $this yii\web\View */
/* @var $generator yii\gii\generators\crud\Generator */

$modelClass = StringHelper::basename($generator->modelClass);
$searchModelClass = StringHelper::basename($generator->searchModelClass);
if ($modelClass === $searchModelClass) {
    $modelAlias = $modelClass . 'Model';
}
$rules = $generator->generateSearchRules();
$labels = $generator->generateSearchLabels();
$searchAttributes = $generator->getSearchAttributes();
$searchConditions = $generator->generateSearchConditions();

echo "<?php\n";
?>

namespace <?= StringHelper::dirname(ltrim($generator->searchModelClass, '\\')) ?>;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use <?= ltrim($generator->modelClass, '\\') . (isset($modelAlias) ? " as $modelAlias" : "") ?>;

/**
 * <?= $searchModelClass ?> represents the model behind the search form about `<?= $generator->modelClass ?>`.
 */
class <?= $searchModelClass ?> extends <?= isset($modelAlias) ? $modelAlias : $modelClass ?>

{
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            <?= implode(",\n            ", $rules) ?>,
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = <?= isset($modelAlias) ? $modelAlias : $modelClass ?>::find();

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        $this->load($params);
        if (!$this->validate()) {
            $query->where('1=0');
            return $dataProvider;
        }

        <?= implode("\n        ", $searchConditions) ?>

        return $dataProvider;
    }



    public function loadData($request)
    {
        $query = <?= isset($modelAlias) ? $modelAlias : $modelClass ?>::find();
        $query->asArray();
        // searching
        if (($q = $request->get('q'))) {
            <?php
            foreach ($searchAttributes as $fieldSearch) {
                echo "\$query->orWhere(['like','".$fieldSearch."', \$q]);\n\t\t\t";
            }
            ?>
}

        // sorting
        if (($sort = $request->get('sort'))) {
            $order = $request->get('order', 'asc');
            $query->orderBy([$sort => $order == 'asc' ? SORT_ASC : SORT_DESC]);
        }

        // paging
        $limit = $request->get('rows') ? 10 : $request->get('rows');
        if ($limit) {
            $page = $request->get('page', 1);
            $total = $query->count();
            $query->offset(($page - 1) * $limit)->limit($limit);
            return[
                'total' => $total,
                'rows' => $query->all(),
            ];
        }
        return $query->all();
    }

}
